package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LHCBetListActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.GetLotteryDetailResponse;
import com.nex3z.flowlayout.FlowLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class LHCBetListAdapter extends BaseAdapter {
    private Context mContext;
    private GetLotteryDetailResponse.ColorBean colorBean;
    private LHCBetListActivity lhcBetListActivity;
    private int type;

    public LHCBetListAdapter(Context mContext, GetLotteryDetailResponse.ColorBean colorBean, LHCBetListActivity lhcBetListActivity, int type) {
        this.mContext = mContext;
        this.colorBean = colorBean;
        this.lhcBetListActivity = lhcBetListActivity;
        this.type = type;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.lhc_bet_list_item, null);
            holder = new ViewHolder(view);
            holder.mWatcher = new MutableWatcher(holder);
            holder.moneyPerNote.addTextChangedListener(holder.mWatcher);
            holder.moneyPerNote.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {// 获得焦点
                        holder.moneyPerNote.setSelection(holder.moneyPerNote.getText().toString().length());
                    }
                }
            });
            view.setTag(holder);
        }

        if(LHCBetListActivity.liuheBeanArrayList.get(i).number != null){
            String[] s = LHCBetListActivity.liuheBeanArrayList.get(i).number.split(",");
            if(type == 0){
                if(!s[0].equals("") && Utils.isNumeric(s[0])){
                    holder.container.removeAllViews();
                    holder.container.setVisibility(View.VISIBLE);
                    holder.number.setVisibility(View.GONE);

                    for(String p : s){
                        AreaCircle areaCircle = new AreaCircle(mContext);
                        if(colorBean != null && colorBean.getRed().contains(p)){
                            areaCircle.setBackground(Color.parseColor("#cf5b5b"));
                        }
                        if(colorBean != null && colorBean.getBlue().contains(p)){
                            areaCircle.setBackground(Color.parseColor("#5773c2"));
                        }
                        if(colorBean != null && colorBean.getGreen().contains(p)){
                            areaCircle.setBackground(Color.parseColor("#7eb379"));
                        }
                        areaCircle.setText(p);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 20), Utils.dip2px(mContext, 20));
                        layoutParams.rightMargin = Utils.dip2px(mContext, 6);
                        areaCircle.setLayoutParams(layoutParams);
                        holder.container.addView(areaCircle);
                    }
                } else {
                    holder.container.setVisibility(View.GONE);
                    holder.number.setVisibility(View.VISIBLE);
                    holder.number.setText(LHCBetListActivity.liuheBeanArrayList.get(i).number);
                }
            } else if(type == 1){
                holder.container.setVisibility(View.GONE);
                holder.number.setVisibility(View.GONE);
            }
        }

//        holder.odd.setText(Html.fromHtml("<font color='#989898'>赔率</font>" + LHCBetListActivity.liuheBeanArrayList.get(i).odd));
        holder.descp.setText(Html.fromHtml(LHCBetListActivity.liuheBeanArrayList.get(i).title + "  " + "<font color='#989898'>赔率</font>" + LHCBetListActivity.liuheBeanArrayList.get(i).odd + "  <font color='#989898'>单注</font>"));
        holder.mWatcher.setActive(false);
        holder.moneyPerNote.setText(LHCBetListActivity.liuheBeanArrayList.get(i).single + "");
        holder.mWatcher.setPosition(i);
        holder.mWatcher.setActive(true);
        if(LHCBetListActivity.liuheBeanArrayList.get(i).quantity == 1) {
            holder.descp_total.setVisibility(View.GONE);
        } else {
            holder.descp_total.setVisibility(View.VISIBLE);
            holder.descp_total.setText(Html.fromHtml("<font color='#989898'>共</font>" + LHCBetListActivity.liuheBeanArrayList.get(i).quantity + "注" + LHCBetListActivity.liuheBeanArrayList.get(i).total + "元"));
        }

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LHCBetListActivity.liuheBeanArrayList.remove(i);
                lhcBetListActivity.recordMoneyList.remove(i);
                notifyDataSetChanged();

                lhcBetListActivity.setMoney();
            }
        });

        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        return LHCBetListActivity.liuheBeanArrayList.size();
    }

    static class ViewHolder{
        @BindView(R.id.number)
        TextView number;
//        @BindView(R.id.odd)
//        TextView odd;
        @BindView(R.id.descp_total)
        TextView descp_total;
        @BindView(R.id.descp)
        TextView descp;
        @BindView(R.id.container)
        FlowLayout container;
        @BindView(R.id.delete)
        ImageView delete;
        @BindView(R.id.moneyPerNote)
        EditText moneyPerNote;

        public MutableWatcher mWatcher;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class MutableWatcher implements TextWatcher {

        private int mPosition;
        private boolean mActive;
        private ViewHolder viewHolder;

        public MutableWatcher(ViewHolder viewHolder){
            this.viewHolder = viewHolder;
        }

        void setPosition(int position) {
            mPosition = position;
        }

        void setActive(boolean active) {
            mActive = active;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }

        @Override
        public void afterTextChanged(Editable s) {
            if (mActive) {
                if(s.length() == 0){
                    LHCBetListActivity.liuheBeanArrayList.get(mPosition).single = 0;
                } else {
                    LHCBetListActivity.liuheBeanArrayList.get(mPosition).single = Integer.parseInt(s.toString());
                }
                LHCBetListActivity.liuheBeanArrayList.get(mPosition).total = LHCBetListActivity.liuheBeanArrayList.get(mPosition).single * LHCBetListActivity.liuheBeanArrayList.get(mPosition).quantity;

                if(LHCBetListActivity.liuheBeanArrayList.get(mPosition).quantity != 1) {
                    viewHolder.descp_total.setText(Html.fromHtml("<font color='#989898'>共</font>" + LHCBetListActivity.liuheBeanArrayList.get(mPosition).quantity + "注" + LHCBetListActivity.liuheBeanArrayList.get(mPosition).total + "元"));
                }
//                notifyDataSetChanged();

                lhcBetListActivity.setMoney();
            }
        }
    }
}

